import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ytbusiness/page/auto_makers/perfect_images/id_pic.dart';
import 'package:flutter/material.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:intl/intl.dart';
import 'package:ytbusiness/widgets/widgets.dart';

///个人实名认证
class PersonalRealName extends StatefulWidget {
  @override
  _PersonalRealNameState createState() => _PersonalRealNameState();
}

class _PersonalRealNameState extends State<PersonalRealName> {
  TextEditingController _nameController = TextEditingController();//姓名
  File iDFront;//身份证正面
  File iDReverse;//身份证反面
  bool _sex;//选择性别
  String _dateOfBirth = "";//出生日期
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: (){
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 0.4,
          centerTitle: true,
          backgroundColor: Colors.white,
          leading: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: (){
              Navigator.pop(context);
            },
            child: Row(
              children: [
                Container(
                    margin: EdgeInsets.only(left: setWidth(15)),
                    width: setWidth(10),
                    height: setWidth(17),
                    child: Image.asset("assets/images/return.png",fit: BoxFit.cover,)
                ),
              ],
            ),
          ),
          title: Text("个人实名认证",style: TextStyle(color: Color(0xff333333),fontSize: setSp(18)),),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              //个人实名认证
              Container(
                margin: EdgeInsets.only( left: setWidth(15),top: setWidth(40),right: setWidth(15)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Text(
                        "个人实名认证",
                        style: TextStyle(
                            color: YTTheme.theme,
                            fontSize: setSp(15),
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                    SizedBox(
                      height: setWidth(24),
                    ),
                    //身份證照片
                    IDPic(iDFront:iDFront,iDReverse:iDReverse,getIDFront: getIDFront,getIDReverse: getIDReverse,),
                    //姓名
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color(0xffF0F0F0),
                              width: 0.5,),
                          )
                      ),
                      padding: EdgeInsets.only(top: setWidth(5),bottom: setWidth(5)),

                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("姓名",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
                          ),
                          SizedBox(
                            width: setWidth(50),
                          ),
                          Expanded(
                              child: Container(
                                child: TextField(
                                  controller: _nameController,
                                  textAlign: TextAlign.end,
                                  maxLines: 1,
                                  keyboardType: TextInputType.multiline,
                                  autofocus: false,
                                  decoration: InputDecoration(
                                      border: InputBorder.none,
                                      focusedBorder: InputBorder.none,
                                      enabledBorder: InputBorder.none,
                                      hintText: "请输入姓名",
                                      hintStyle: TextStyle(color: Color(0xff989898),fontSize: setSp(15))
                                  ),
                                ),

                              )),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only( left: setWidth(15),right: setWidth(15)),
                child: Column(
                  children: [
                    //身份证号
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color(0xffF0F0F0),
                              width: 0.5,),
                          )
                      ),
                      padding: EdgeInsets.only(top: setWidth(5),bottom: setWidth(5)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("身份证号",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
                          ),
                          SizedBox(
                            width: setWidth(50),
                          ),
                          Expanded(
                              child: Container(
                                child: TextField(
                                  textAlign: TextAlign.end,
                                  maxLines: 1,
                                  keyboardType: TextInputType.multiline,
                                  autofocus: false,
                                  inputFormatters: [
                                    WhitelistingTextInputFormatter(RegExp("[0-9.]|[a-zA-Z]")),//只允许输入数字字母
                                  ],
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    focusedBorder: InputBorder.none,
                                    enabledBorder: InputBorder.none,
                                    hintText: "请输入身份证号码",
                                    hintStyle: TextStyle(color: Color(0xff989898),fontSize: setSp(16)),

                                  ),
                                ),
                              )),
                        ],
                      ),
                    ),
                    //住址
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color(0xffF0F0F0),
                              width: 0.5,),
                          )
                      ),
                      padding: EdgeInsets.only(top: setWidth(5),bottom: setWidth(5)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("住址",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
                          ),
                          SizedBox(
                            width: setWidth(50),
                          ),
                          Expanded(
                              child: Container(
                                child: TextField(
                                  textAlign: TextAlign.end,
                                  maxLines: null,
                                  keyboardType: TextInputType.multiline,
                                  autofocus: false,
                                  decoration: InputDecoration(
                                    border: InputBorder.none,
                                    focusedBorder: InputBorder.none,
                                    enabledBorder: InputBorder.none,
                                    hintText: "请输入住址",
                                    hintStyle: TextStyle(color: Color(0xff989898),fontSize: setSp(16)),

                                  ),
                                ),
                              )),
                        ],
                      ),
                    ),
                    //性别
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color(0xffF0F0F0),
                              width: 0.5,),
                          )
                      ),
                      padding: EdgeInsets.only(top: setWidth(21),bottom: setWidth(21)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("性别",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
                          ),
                          GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: (){
                              customDialog(context);
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Container(
                                  child: Text(_sex == null?"请选择性别":(_sex? "女" : "男"),style: TextStyle(color: Color(0xff989898),fontSize: setSp(15)),),
                                ),
                                SizedBox(
                                  width: setWidth(3),
                                ),
                                Container(
                                  width: setWidth(6),
                                  height: setWidth(11),
                                  child: Image.asset("assets/images/right.png",fit: BoxFit.cover,),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    //出生日期
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color(0xffF0F0F0),
                              width: 0.5,),
                          )
                      ),
                      padding: EdgeInsets.only(top: setWidth(21),bottom: setWidth(21)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text("出生日期",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
                          ),
                          GestureDetector(
                            onTap: (){
                              DatePicker.showDatePicker(context,
                                  // 是否展示顶部操作按钮
                                  showTitleActions: true,
                                  // change事件
                                  onChanged: (date) {
                                    print('change $date');
                                  },
                                  // 确定事件
                                  onConfirm: (date) {
                                    _dateOfBirth = DateFormat("yyyy-MM-dd").format(date);
                                    print('confirm $date');
                                  },
                                  // 当前时间
                                  currentTime: DateTime.now(),
                                  // 语言
                                  locale: LocaleType.zh);

                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                SizedBox(
                                  width: setWidth(3),
                                ),
                                Container(
                                  child: Text(_dateOfBirth != ""?_dateOfBirth:"请输入出生日期",style: TextStyle(color: Color(0xff989898),fontSize: setSp(15)),),
                                ),
                                SizedBox(
                                  width: setWidth(3),
                                ),
                                Container(
                                  width: setWidth(6),
                                  height: setWidth(11),
                                  child: Image.asset("assets/images/right.png",fit: BoxFit.cover,),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: setWidth(80),
              )
            ],
          ),
        ),
          //提交
          bottomSheet:confirmButton(
              title: "确定",
              voidCallback: (){
                Navigator.pop(context);

              })
      ),
    );
  }

  ///自定义弹窗  选择性别
  customDialog(BuildContext context) {
    showDialog<Null>(
      context: context,
      builder: (BuildContext context) {
        return new SimpleDialog(
          backgroundColor: Colors.transparent,
          titlePadding: EdgeInsets.all(0),
          contentPadding: EdgeInsets.all(0),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10))),
          children: <Widget>[
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: StatefulBuilder(
                builder: (BuildContext context, StateSetter state) {
                  return Container(
                    decoration:
                    BoxDecoration(borderRadius: BorderRadius.circular(8)),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            state(() {
                              setState(() {
                                _sex = true;
                              });
                            });
                            Navigator.pop(context);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                                color: Colors.white,
//                                color: _sex == null ?Colors.white:(_sex ? YTTheme.theme: Colors.white),
                                borderRadius: BorderRadius.only(
                                    topRight: Radius.circular(8),
                                    topLeft: Radius.circular(8))),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Expanded(
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(
                                      "女",
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: setSp(15)),
                                    ),
                                    padding: EdgeInsets.only(
                                        top: setWidth(15),
                                        bottom: setWidth(15)),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            state(() {
                              setState(() {
                                _sex = false;
                              });
                            });
                            Navigator.pop(context);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                                color: Colors.white,
//                                color: _sex == null ?Colors.white:(_sex ?Colors.white:  YTTheme.theme),
                                borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(8),
                                    bottomRight: Radius.circular(8))),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Expanded(
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(
                                      "男",
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: setSp(15)),
                                    ),
                                    padding: EdgeInsets.only(
                                        top: setWidth(15),
                                        bottom: setWidth(15)),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        );
      },
    );
  }
  //身份证正面
  getIDFront(File images){
    setState(() {
      iDFront = images;
    });

  }

  //身份证反面
  getIDReverse(File images){
    setState(() {
      iDReverse = images;
    });

  }

}
